<template>
	<view>
		<app-header title="管理">
			<uv-icon name="bell" size="22" color="#fff"></uv-icon>
			<!-- 名称 -->
			<view class="mc" @click="navigateTo('/pages/manage/mbi')">
				<image class="mc-img" src="/static/imgs/managerCow.png"></image>
				<view class="text">
					<view class="name">
						名称名称
					</view>
					<view class="number">
						18612345698
					</view>
				</view>
				<view class="arrow-right">
				  <uv-icon name="arrow-right" size="30" color="white"></uv-icon>
				</view>
			</view>
			<view class="box">
				<view v-for="(item, index) in data" :key="index">
					<BoxItem :item="item" />
				</view>
			</view>
		</app-header>
		<view class="body">
			<view v-for="(item, index) in xxList" :key="index">
				<ManageItem :item="item" />
			</view>
		</view>
	</view>
</template>

<script setup>
	import AppHeader from '@/components/AppHeader/AppHeader.vue';
	import BoxItem from '@/components/homeson/BoxItem.vue';
	import ManageItem from '@/components/ManageItem/ManageItem.vue';
	import { getHealthInfo } from '@/api/manageLeft'
	import { ref, onMounted } from 'vue'
	
	// 定义响应式变量
	const healthData = ref({
		countNum: 0,
		healthNum: 0,
		unHealthNum: 0
	});
	
	// 初始数据（接口未返回数据时的占位数据）
	const data = ref([
		{
			number: healthData.value.countNum,
			title: '牲畜数量'
		},
		{
			number: healthData.value.healthNum,
			title: '健康数量'
		},
		{
			number: healthData.value.unHealthNum,
			title: '异常数量'
		}
	]);
	
	const xxList = [
		{
			img: '/static/manage/cs.png',
			text: '牲畜信息',
			url: '/pages/manage/lhi'
		},
		{
			img: '/static/manage/quarantine.png',
			text: '检疫管理',
			url: '/pages/manage/quarantine/quarantinehome'
		},
		{
			img: '/static/manage/vaccine.png',
			text: '疫苗管理',
			url: '/pages/manage/vaccine/vaccine'
		},
		{
			img: '/static/manage/hybridization.png',
			text: '配种管理',
			url: '/pages/manage/mating/matinghome'
		},
		{
			img: '/static/manage/breed.png',
			text: '繁殖管理',
			url: '/pages/manage/reproduce/Repgl'
		},
		{
			img: '/static/manage/mc.png',
			text: '牧场信息',
			url: ''
		},
		{
			img: '/static/manage/video.png',
			text: '视频监控',
			url: '/pages/manage/vs'
		},
		{
			img: '/static/manage/alarm.png',
			text: '报警记录',
			url: '/pages/home/notifications'
		}
	];
	
	// 获取健康动物数量
	const fetchHealthInfo = async () => {
	  try {
	    const res = await getHealthInfo();
	    console.log("管理首页健康动物数量返回数据：", res);
	    if (res.code === 200 && res.data) {
	      // 更新响应式数据
	      healthData.value = res.data;
	      // 更新显示的数据
	      data.value = [
	        {
	          number: res.data.countNum,
	          title: '牲畜数量'
	        },
	        {
	          number: res.data.healthNum,
	          title: '健康数量'
	        },
	        {
	          number: res.data.unHealthNum,
	          title: '异常数量'
	        }
	      ];
	    }
	  } catch (error) {
	    console.error("获取管理首页健康动物数量失败：", error);
	  }
	};
	
	const navigateTo = (url) => {
		uni.navigateTo({
			url: url
		});
	};
	
	onMounted(() => {
		fetchHealthInfo();
	})
</script>

<style lang="scss">
.mc{
	width: 750rpx;
	height: 130rpx;
	position: absolute;
	left:-40rpx;
	top:100rpx;
	display: flex;
	.mc-img{
		width: 130rpx;
		height: 130rpx;
		margin-left: 30rpx;
	}
	.text{
		margin-left: 10rpx;
		flex:1;
		.name{
			font-size: 36rpx;
			margin-top: 12rpx;
			margin-bottom: 5rpx;
		}
	}
	.arrow-right{
	  width: 60rpx;
	  height: 100%;
	  display: flex;
	  align-items: center;
	  margin-right: 18rpx;
	}
}

.box {
	width: 690rpx;
	height: 138rpx;
	margin-top: 220rpx;
	background-color: white;
	position: absolute;
	display: flex;
	padding-top: 40rpx;
	padding-bottom: 30rpx;
	border-radius: 20rpx;
	box-shadow:0 0 5px rgba(0, 0, 0, 0.3);
	.sl{
		margin-top: 40rpx;
		margin-bottom: 30rpx;
	}
}

.body{
	width: 626rpx;
	height: 320rpx;
	margin-top: 140rpx;
	margin-left: auto;
	margin-right: auto;
	display: flex;
	justify-content: space-between;
	flex-wrap: wrap;
	.item{
		width: calc(25% - 20rpx);
		height: 137rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 44rpx;
		.item-img{
			width: 87rpx;
			height: 87rpx;
		}
		.xx{
			font-size: 23rpx;
		}
	}
}
</style>